/*
 *  Copyright 2008 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.antext.core;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import com.blandware.atleap.antext.core.generate.StringTransformer;

/**
 * Task which is used to convert strings.
 * <p><a href="TransformStringTask.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2008/05/15 17:45:25 $
 */
public class TransformStringTask extends Task {

    public static final String OPERATION_CAPITALIZE = "capitalize";
    public static final String OPERATION_DECAPITALIZE = "decapitalize";
    public static final String OPERATION_PLURALIZE = "pluralize";
    public static final String OPERATION_LOWERCASE = "lowercase";
    public static final String OPERATION_UPPERCASE = "uppercase";

    protected String property;
    protected String operation;
    protected String arg1;

    private final StringTransformer stringTransformer = new StringTransformer();

    /**
     * Sets name of property to which to assign result.
     *
     * @param property    property name
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * Sets operation.
     *
     * @param operation operation to perform
     */
    public void setOperation(String operation) {
        this.operation = operation;
    }

    /**
     * Sets first argument.
     *
     * @param arg1  some value
     */
    public void setArg1(String arg1) {
        this.arg1 = arg1;
    }

    public void execute() throws BuildException {
        validateSettings();
        getProject().setProperty(property, transform());
    }

    /**
     * Transforms string.
     *
     * @return operation result
     */
    protected String transform() {
        String result;
        if (OPERATION_CAPITALIZE.equalsIgnoreCase(operation)) {
            result = stringTransformer.capitalize(arg1);
        } else if (OPERATION_DECAPITALIZE.equalsIgnoreCase(operation)) {
            result = stringTransformer.decapitalize(arg1);
        } else if (OPERATION_PLURALIZE.equalsIgnoreCase(operation)) {
            result = stringTransformer.pluralize(arg1);
        } else if (OPERATION_LOWERCASE.equalsIgnoreCase(operation)) {
            result = arg1.toLowerCase();
        } else if (OPERATION_UPPERCASE.equalsIgnoreCase(operation)) {
            result = arg1.toUpperCase();
        } else {
            throw new BuildException("Unknown operation: " + operation);
        }
        return result;
    }

    /**
     * Validates settings and throws exception if they are not valid.
     *
     * @throws BuildException   thrown if not valid
     */
    protected void validateSettings() throws BuildException {
        if (property == null || property.trim().length() == 0) {
            throw new BuildException("Specify resultProperty");
        }
        if (operation == null || operation.trim().length() == 0) {
            throw new BuildException("Specify operation");
        }
        if (arg1 == null) {
            throw new BuildException("Specify arg1");
        }
    }
}
